
package com.huawei.demo;

import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.http.HttpConfig;
import com.huaweicloud.sdk.drs.v3.DrsClient;
import com.huaweicloud.sdk.drs.v3.model.BatchListJobStatusRequest;
import com.huaweicloud.sdk.drs.v3.model.BatchListJobStatusResponse;
import com.huaweicloud.sdk.drs.v3.model.BatchQueryJobReqPage;
import com.huaweicloud.sdk.drs.v3.model.CreateCompareTaskReq;
import com.huaweicloud.sdk.drs.v3.model.CreateCompareTaskRequest;
import com.huaweicloud.sdk.drs.v3.model.CreateCompareTaskResponse;
import com.huaweicloud.sdk.drs.v3.model.CreateCompareTaskResult;
import com.huaweicloud.sdk.drs.v3.model.ListCompareResultRequest;
import com.huaweicloud.sdk.drs.v3.model.ListCompareResultResponse;
import com.huaweicloud.sdk.drs.v3.model.QueryCompareResultReq;
import com.huaweicloud.sdk.drs.v3.model.QueryJobsReq;
import com.huaweicloud.sdk.drs.v3.region.DrsRegion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public class CreateCompareTaskDemo {
    private static final Logger logger = LoggerFactory.getLogger(CreateCompareTaskDemo.class.getName());

    public static void main(String[] args) {
        // 创建认证
        String ak = "<YOUR AK>";
        String sk = "<YOUR SK>";

        ICredential auth = new BasicCredentials().withAk(ak).withSk(sk);
        // 配置客户端属性
        HttpConfig config = HttpConfig.getDefaultHttpConfig();
        config.withIgnoreSSLVerification(true);
        // 创建DrsClient实例
        DrsClient client = DrsClient.newBuilder().withCredential(auth).withHttpConfig(config)
                .withRegion(DrsRegion.CN_NORTH_4).build();
        CreateCompareTaskRequest createCompareTaskRequest = new CreateCompareTaskRequest();
        // 查询任务状态
        String status = getStatus(client);
        // 增量中才能进行对比
        if (QueryJobsReq.StatusEnum.INCRE_TRANSFER_STARTED.getValue().equals(status)) {
            CreateCompareTaskReq createCompareTaskReq = new CreateCompareTaskReq();
            createCompareTaskReq.withJobId("<YOUR JOB ID>");
            // 创建对象对比
            createCompareTaskReq.withObjectLevelCompareType(CreateCompareTaskReq.ObjectLevelCompareTypeEnum.OBJECTS);
            createCompareTaskRequest.setBody(createCompareTaskReq);
            CreateCompareTaskResponse compareTask = client.createCompareTask(createCompareTaskRequest);
            logger.info(compareTask.toString());
            // 创建成功后获取对比任务ID，调用查询对比结果接口
            getCompareResult(client, compareTask);
        }
    }

    /**
     * 查询任务状态
     * 
     * @param client
     * @return
     */
    private static String getStatus(DrsClient client) {
        BatchListJobStatusRequest batchListProgressesRequest = new BatchListJobStatusRequest();
        BatchQueryJobReqPage batchQueryJobReqPage = new BatchQueryJobReqPage();
        List<String> jobs = new ArrayList<>();
        jobs.add("<YOUR JOB ID>");
        batchQueryJobReqPage.withJobs(jobs);
        batchListProgressesRequest.setBody(batchQueryJobReqPage);
        BatchListJobStatusResponse batchListJobStatusResponse = client.batchListJobStatus(batchListProgressesRequest);
        return batchListJobStatusResponse.getResults().get(0).getStatus().getValue();
    }

    /**
     * 查询对比结果
     * 
     * @param client
     * @param compareTask
     */
    private static void getCompareResult(DrsClient client, CreateCompareTaskResponse compareTask) {
        if (compareTask.getHttpStatusCode() == 200) {
            CreateCompareTaskResult dataLevelCompareCreateResult = compareTask.getObjectLevelCompareCreateResult();
            String compareTaskId = dataLevelCompareCreateResult.getCompareTaskId();
            ListCompareResultRequest request = new ListCompareResultRequest();
            QueryCompareResultReq queryCompareResultReq = new QueryCompareResultReq();
            queryCompareResultReq.withJobId("<YOUR JOB ID>");
            queryCompareResultReq.withObjectLevelCompareId(compareTaskId);
            queryCompareResultReq.withCurrentPage(1);
            queryCompareResultReq.withPerPage(10);
            request.withBody(queryCompareResultReq);
            ListCompareResultResponse listCompareResultResponse = client.listCompareResult(request);
            logger.info(listCompareResultResponse.toString());
        }
    }
}
